#!/bin/bash
#
# emqx
#
# chkconfig: 2345 80 30
# description: EMQX, a distributed, massively scalable, highly extensible MQTT message broker written in Erlang/OTP
# processname: beam
#

# Source function library.
# shellcheck disable=SC1091
. /etc/rc.d/init.d/functions

RETVAL=0
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=emqx
DAEMON=/usr/bin/$NAME
lockfile=/var/lock/subsys/$NAME
mkdir -p /var/run/$NAME
pidfile=/var/run/$NAME/$NAME.pid

# Check for script, config and data dirs
[ -x /usr/bin/$NAME ] || exit 0
[ -d /etc/$NAME ] || exit 0
[ -d /var/lib/$NAME ] || exit 0

# Read configuration variable file if it is present and readable
# shellcheck disable=SC1090
[ -r /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME

# `service` strips all environmental VARS so
# if no HOME was set in /etc/sysconfig/$NAME then set one here
# to the data directory for erlexec's sake
if [ -z "$HOME" ]; then
    export HOME=
fi

status -p $pidfile -l "$(basename $lockfile)" $NAME >/dev/null 2>&1
running=$?

find_pid() {
    # shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions
    ps ax | grep -E "\-progname\s+$NAME\s" | awk '{print $1}'
}

check_pid_status() {
    local pid
    pid="$(find_pid)"
    if [ "$pid" = "" ]; then
        # prog not running?
        return 1
    else
        # running
        return 0
    fi
}

start() {
    # Start daemons.
    echo -n $"Starting emqx: "
    $DAEMON start
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        touch $lockfile
        find_pid > $pidfile
        success
    else
        failure $"$NAME start"
    fi
    echo
    return $RETVAL
}

stop() {
    # Stop daemon.
    echo -n $"Shutting down emqx: "
    $DAEMON stop 2>/dev/null
    for _ in $(seq 1 10); do
        sleep 1
        check_pid_status
        RETVAL=$?
        if [ $RETVAL -eq 1 ]; then
            break
        fi
    done
    if [ $RETVAL -eq 1 ]; then
        rm -f $lockfile $pidfile
        success
        echo && return 0
    else
        failure $"$NAME stop"
        echo && return 1
    fi
}

hardstop() {
    echo -n $"Shutting down $NAME: "
    su - emqx -c "ps -ef | grep -E '\-progname\s+$NAME\s' | awk '{print \$2}' | xargs kill -9"
    for _ in $(seq 1 10); do
        sleep 1
        check_pid_status
        RETVAL=$?
        if [ $RETVAL -eq 1 ]; then
            break
        fi
    done
    if [ $RETVAL -eq 1 ]; then
        rm -f $lockfile $pidfile
        success
        echo && return 0
    else
        failure $"$NAME hardstop"
        echo && return 1
    fi
}

# See how we were called.
case "$1" in
    start)
        [ $running -eq 0 ] && exit 0
        start
        ;;
    stop)
        stop
        ;;
    restart|force-reload)
        [ $running -eq 0 ] && stop
        start
        ;;
    hardstop)
        [ $running -eq 0 ] || exit 0
        hardstop
        ;;
    condrestart|try-restart)
        [ $running -eq 0 ] || exit 0
        restart
        ;;
    status)
        status -p $pidfile -l "$(basename $lockfile)" $NAME
        ;;
    ping)
        $DAEMON ping || exit $?
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|force-reload|hardstop|condrestart|try-restart|status|ping}"
        exit 1
esac

exit $?
